<?php

//Librairie de fonctions
//require("lib/libsession.inc.php");
//require("lib/libfonctions.inc.php");
require("lib/libencode.inc.php");
require("lib/libmysql.inc.php");

session_start();
setConnectionSql();
if (isset($_POST['prm1']) && isset($_POST['prm2'])) {
    $rsearch1 = antiInjection($_POST['prm1']);
    $rsearch2 = antiInjection($_POST['prm2']);
    if (isset($_POST['prm3']) && !empty($_POST['prm3'])) {
        $rsearch3 = antiInjection($_POST['prm3']);
        if ($rsearch3 != "--------") {
            $prm3vide = false;
        } else {
            unset($rsearch3);
            $prm3vide = true;
        }
    } else {
        $prm3vide = true;
    }
    if (isset($_POST['prm4']) && !empty($_POST['prm4'])) {
        $rsearch4 = antiInjection($_POST['prm4']);
        if ($rsearch4 == "1" || $rsearch4 == "2") {
            $prm4vide = false;
        } else {
            unset($rsearch4);
            $prm4vide = true;
        }
    } else {
        $prm4vide = true;
    }
    if ($rsearch1 != "" && $rsearch2 != "") {
        unset($sqlparam);
        $sqlparam["table"][] = "liberations";
        $sqlparam["table"][] = "ligneliberations";
        $sqlparam["table"][] = "articles";
        $sqlparam["table"][] = "duree";
        $sqlparam["join"][] = "ligneliberations.refliberation = liberations.refliberation";
        $sqlparam["join"][] = "articles.refarticle=ligneliberations.refarticle";
        $sqlparam["join"][] = "duree.refduree=ligneliberations.duree";
        $sqlparam["champs"][] = "liberations.refemploye";
        $sqlparam["champs"][] = "articles.refarticle";
        $sqlparam["champs"][] = "unix_timestamp(ligneliberations.dateliberation) as dateliberation";
        $sqlparam["champs"][] = "SUM(If(liberations.typeoperation=1,duree.dureeNombre,0-dureeNombre)) AS calculduree";
        $sqlparam["groupe"][] = "liberations.refemploye";

        $sqlparam["groupe"][] = "ligneliberations.dateliberation";
        if (!$prm3vide) {
            $sqlparam["where"][] = sprintf("ligneliberations.refarticle=%d", $rsearch3);
            $sqlparam["groupe"][] = "articles.refarticle";
        }

        $sqlparam["where"][] = "DATE_FORMAT(ligneliberations.dateliberation,'%Y-%m-%d')='$rsearch2'";
        $sqlparam["where"][] = "liberations.refemploye=$rsearch1";
        $resultat = executerRequeteSql($sqlparam);

        if (is_array($resultat) && count($resultat) > 0) {
            unset($tablovide);
            $tablovide = true;

            unset($somme_temps);
            $somme_temps = 0;

            foreach ($resultat as $clef => $valeur) {
                $somme_temps += floatval($valeur["calculduree"]);
            }
            if ($somme_temps <= 0) {
                if ($rsearch4 == "2") {
                    if (!$prm3vide) {
                        print "occupedatearticle_annulation";
                    } else {
                        print "occupedate_annulation";
                    }
                } else {
                    print "accepte";
                }
            } elseif ($somme_temps > 0) {
                if ($rsearch4 == "1") {
                    if (!$prm3vide) {
                        print "occupedatearticle_demande";
                    } else {
                        print "occupedate_demande";
                    }
                } else {
                    print "accepte";
                }
            }
        } else {
            if ($rsearch4 == "2") {
                print "refus_annulation";
            } else {
                print "accepte";
            }
        }
    }
}
mysql_close();
